package com.zx.idc.backend.gui.base.house;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zx.idc.backend.gui.common.BaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.common.base.BaseExcelApi;
import com.zx.idc.common.util.DealObjectNullValue;
import com.zx.idc.ds.base.cus.entity.BaseCustomer;
import com.zx.idc.ds.base.house.entity.BaseFrameInfo;
import com.zx.idc.ds.base.house.service.IBaseFrameInfoService;
import com.zx.idc.ds.base.house.vo.BaseFrameInfoVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 机架信息管理 前端控制器
 * </p>
 *
 * @author rxliuli
 * @since 2019-01-31
 */
@RestController
@RequestMapping("/api/baseFrameInfo")
public class BaseFrameInfoController extends BaseApi implements BaseExcelApi<IBaseFrameInfoService, BaseFrameInfoVo> {

    @Autowired
    private IBaseFrameInfoService service;

    /**
     * 根据客户信息查询机架列表
     *
     * @param baseCustomer 客户信息
     * @return 机架列表
     */
    @GetMapping("/list-by-customer")
    @RequiresPermissions("basic-data:base-frame-info")
    public Result<List<BaseFrameInfo>> listByCustomer(BaseCustomer baseCustomer) {
        return success(service.listByCustomer(baseCustomer));
    }

    @GetMapping("/get/{id}")
    @RequiresPermissions("basic-data:base-frame-info")
    public Result<BaseFrameInfoVo> get(@PathVariable("id") Long id) {
        return success(service.getById(id));
    }

    @GetMapping("/query-page")
    @RequiresPermissions("basic-data:base-frame-info")
    public Result<Page<BaseFrameInfoVo>> page(Page<BaseFrameInfoVo> page, BaseFrameInfoVo baseServerInfo) {
        return success(service.queryPage(page, baseServerInfo));
    }

    @PostMapping("/add")
    @RequiresPermissions("basic-data:base-frame-info:edit")
    public Result<Boolean> add(@RequestBody BaseFrameInfoVo baseFrameInfoVo) {
        String msg;
        try {
            return build(service.insertBaseFrameInfo(baseFrameInfoVo));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("新增机架异常", e);
        }
        return error(msg);
    }

    @PostMapping("/update")
    @RequiresPermissions("basic-data:base-frame-info:edit")
    public Result<Boolean> update(@RequestBody BaseFrameInfoVo baseFrameInfoVo) {
        String msg;
        try {
            return build(service.updateBaseFrameInfo(DealObjectNullValue.setSuperClassNullValue(baseFrameInfoVo)));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("修改机架异常", e);
        }
        return error(msg);
    }

    @PostMapping("/delete/batch")
    @RequiresPermissions("basic-data:base-frame-info:edit")
    public Result<Boolean> deleteBatch(@RequestBody List<Long> ids) {
        String msg;
        try {
            return build(service.deleteBatchByIds(ids));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("删除机架异常", e);
        }
        return error(msg);
    }

    @GetMapping("/select-list")
    @RequiresPermissions("basic-data:base-frame-info")
    public Result<List<BaseFrameInfo>> selectList() {
        return selectList(new BaseFrameInfo());
    }

    @PostMapping("/select-list-param")
    @RequiresPermissions("basic-data:base-frame-info")
    public Result<List<BaseFrameInfo>> selectList(@RequestBody BaseFrameInfo baseFrameInfo) {
        return success(service.selectList(new EntityWrapper<>(baseFrameInfo).setSqlSelect("id", "frameName")));
    }

    /**
     * 获取一个 Service，由子类实现
     * 实现这个方法可以更快的获取到 Service 以提高响应速度
     *
     * @return 服务类实现对象
     */
    @Override
    public Optional<IBaseFrameInfoService> getService() {
        return Optional.of(service);
    }

    @GetMapping("/area-id/{areaId}/frame-num/{frameNum}")
    @RequiresPermissions("basic-data:base-frame-info")
    public Result<Boolean> selectList(@PathVariable("areaId") Long areaId, @PathVariable("frameNum") String frameNum) {
        return success(
            service.selectCount(new BaseFrameInfo().setHouseArea(areaId).setFrameCode(frameNum)) == 0
        );
    }

    /**
     * 获取机房下的机架信息
     *
     * @return 机架信息
     */
    @GetMapping("/select-list-append-houseName/{idcId}")
    @RequiresPermissions("basic-data:base-frame-info")
    public Result<List<BaseFrameInfoVo>> selectListAppendHouseName(@PathVariable("idcId") String idcId) {
        return success(service.selectListAppendHouseName(idcId));
    }
}

